/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package planspiel.panels.subpanels;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JPanel;

import planspiel.GameRoundController;
import planspiel.panels.GameScreen;
import admintool.imp03_data.IMP03_Candidate;
import admintool.imp03_data.IMP03_Message;
import admintool.imp03_data.IMP03_Platform;
import admintool.imp03_data.IMP03_Vacancy;

/**
 *
 * @author Administrator
 */
public class GameFooter extends JPanel {

	private static final long serialVersionUID = -6421950237354068742L;
	private GameScreen gameScreen = null;
    
        /**
         * 
         * @param gameScreen
         */
        public GameFooter(GameScreen gameScreen) {
        this.gameScreen = gameScreen;
        initDisplay(this.gameScreen);
    }    
    
    private void initDisplay(final GameScreen gameScreen) {
        GroupLayout layout = new GroupLayout(this);
        this.setLayout(layout);
        
        JButton ok = new JButton("ok");
        JButton cancel = new JButton("cancel");

        //Enable / Disable Buttons
        int currentStage = gameScreen.getGame().getCurrentGameRoundController().getSelectedGameStage();
        if(currentStage == GameRoundController.VACANCYSTAGE){
            cancel.setEnabled(false);
            IMP03_Vacancy selectedVacancy = gameScreen.getGame().getCurrentGameRoundController().getSelectedVacancy();
            if(selectedVacancy != null) {
                ok.setEnabled(false);
            }
        } else if(currentStage == GameRoundController.PLATFORMSTAGE){
            cancel.setEnabled(false);
            if(gameScreen.getGame().getCurrentGameRoundController().getSelectedPlatform() != null){
                ok.setEnabled(false);
            }
        } else if(currentStage == GameRoundController.CANDIDATESTAGE){
            cancel.setEnabled(false);
        } else if(currentStage == GameRoundController.MESSAGESTAGE){
            ok.setEnabled(false);
            cancel.setEnabled(false);
        } else if(currentStage == GameRoundController.TIPSTAGE){
            ok.setEnabled(false);
            cancel.setEnabled(false);
        }
        
        //ActionListener
        ok.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                int currentStage = gameScreen.getGame().getCurrentGameRoundController().getSelectedGameStage();
                
                if(currentStage == GameRoundController.VACANCYSTAGE){
                    IMP03_Vacancy vacancy = gameScreen.getContentVacancy().getSelectedVacancy();
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedVacancy(vacancy);
                    gameScreen.getGame().getGameRound().setScore(vacancy.getVacancyBudget());
                    if (gameScreen.getHeader() != null) {
                    	gameScreen.getHeader().getScore().setText(gameScreen.getGame().getGameRound().getScoreAsString());
                    	gameScreen.getGame().getCurrentGameRoundController().startBudgetTimer(gameScreen.getGame().getGameRound());
                    }
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedGameStage(GameRoundController.PLATFORMSTAGE);
                    gameScreen.getGame().getCurrentGameRoundController().setPlatformButtonEnabled(true);
                    gameScreen.getGame().initDisplay();
                } else if(currentStage == GameRoundController.PLATFORMSTAGE){
                    IMP03_Platform platform = gameScreen.getContentPlatform().getSelectedPlatform();
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedPlatform(platform);
                    gameScreen.getGame().getGameRound().reduceScore(platform.getPlatformCost());
                    gameScreen.getHeader().getScore().setText(gameScreen.getGame().getGameRound().getScoreAsString());
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedGameStage(GameRoundController.CANDIDATESTAGE);
                    gameScreen.getGame().getCurrentGameRoundController().setCandidatesButtonEnabled(true);
                    gameScreen.getGame().getCurrentGameRoundController().setMessagesButtonEnabled(true);
                    gameScreen.getGame().initDisplay();
                } else if(currentStage == GameRoundController.CANDIDATESTAGE){
                    IMP03_Candidate candidate = gameScreen.getContentCandidate().getSelectedCandidate();
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedCandidate(candidate);
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedGameStage(GameRoundController.FINISHED);
                    gameScreen.getGame().finishRound();
                } else if(currentStage == GameRoundController.MESSAGESTAGE){
                    IMP03_Message message = gameScreen.getContentMessage().getSelectedMessage();
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedMessage(message);
                    gameScreen.getGame().getCurrentGameRoundController().setSelectedGameStage(GameRoundController.FINISHED);
//                    gameScreen.getGame().finishRound();
                } else if(currentStage == GameRoundController.TIPSTAGE){
                }
            }
        });
        
        layout.setHorizontalGroup(
            layout.createSequentialGroup()
                .addComponent(ok, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
                .addComponent(ok, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cancel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }
    
}
